package com.jagochan.imchat.module.common.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadFactory;

@Slf4j
public class CustomThreadFactory implements ThreadFactory {
    private ThreadFactory originalThreadFactory;

    public CustomThreadFactory(ThreadFactory threadFactory) {
        originalThreadFactory = threadFactory;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = originalThreadFactory.newThread(r);
        thread.setUncaughtExceptionHandler(new CustomThreadUncaughtExceptionHandler());
        return thread;
    }

    public static class CustomThreadUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            log.error("线程池出现异常", e);
        }
    }

}
